<template>
  <div>
    <el-dialog
      title="查看用户角色"
      :visible.sync="dialog.RoleMainShow"
      width="80%"
      append-to-body
    >
      <RoleMain
        ref="RoleMainShow"
        :isMultiple="true"
        :multipleOptions="{optType: 'show'}"
        @showSelectDialog="showSelectDialog"
        @refresh="refresh"
      />
    </el-dialog>
    <el-dialog
      title="新增用户角色"
      :visible.sync="dialog.RoleMainAdd"
      width="80%"
      append-to-body
    >
      <RoleMain
        ref="RoleMainAdd"
        :isMultiple="true"
        :multipleOptions="{optType: 'add'}"
        @closeSelectDialog="closeSelectDialog"
        @refresh="refresh"
      />
    </el-dialog>
  </div>
</template>

<script>
import { doPostJson } from '@/utils/utils'
import * as ResponseStatus from '@/utils/response-status'
import RoleMain from '../Role/RoleMain.vue'
export default {
  name: 'UserRoleDialog',
  components: {
    RoleMain
  },
  data() {
    return {
      dialog: {
        RoleMainShow: false,
        RoleMainAdd: false
      },
      user: {},
      selectedData: []
    }
  },
  computed: {},
  mounted() {},
  methods: {
    initData(user) {
      this.user = user
      this.dialog.RoleMainShow = true
      this.$nextTick(() => {
        let roleMainShow = this.$refs.RoleMainShow
        roleMainShow.extraData = user
        doPostJson(
        '/user-role/admin/all-cond',
        {
          userId: this.user.id,
          isActive: 1
        },
        {}
      )
        .then(res => {
          let roleIds = [0]
          res.data.rows.forEach(row => {
            roleIds.push(row.roleId)
          })
          roleMainShow.resetSearchExcludeProps = ['ids']
          roleMainShow.$refs.searchDialog.searchForm.ids = roleIds
          roleMainShow.$refs.quickSearch.searchForm.ids = roleIds
          roleMainShow.searchTable(false, true)
          this.selectedData = res.data.rows
        })
        .catch(err => {})
      })
    },
    showSelectDialog() {
      this.dialog.RoleMainAdd = true
      this.$nextTick(() => {
        let roleMainAdd = this.$refs.RoleMainAdd
        let roleTable = roleMainAdd.$refs.table
        roleTable.selectedData = this.selectedData
        roleTable.selectedDataIdProp = 'roleId'
        roleTable.extraData.userId = this.user.id
        roleTable.extraData.userPhone = this.user.phone
        roleTable.extraData.userNickname = this.user.nickname
        roleMainAdd.searchTable(false, true)
      })
    },
    closeSelectDialog() {
      this.dialog.RoleMainAdd = false
    },
    refresh() {
      this.initData(this.user)
    }
  }
}
</script>

<style>
</style>
